package cn.signalpha.common.utils;

import cn.signalpha.modules.com.entity.User;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * XML bean生成工具
 *
 * @author signalpha
 */
public class MyBatisUtil {

	/**
	 * 获取bean属性，根据属性生成resultMap
	 */
	public static String getResultMap(Class<?> cls) throws Exception {
		String str = "";
		// 每一行字符串 <result column="BID_SECTION_CODE" property="BID_SECTION_CODE"
		// jdbcType="VARCHAR" />
		String linestr = "";
		Field[] declaredFields = cls.getDeclaredFields();
		for (Field field : declaredFields) {
			if (field.getType().getName().equals("java.lang.String")) {
				linestr = "<result column=\"" + field.getName() + "\" property=\"" + field.getName()
					+ "\" jdbcType=\"VARCHAR\" />";
			} else {
				linestr = "<result column=\"" + field.getName() + "\" property=\"" + field.getName()
					+ "\" jdbcType=\"INTEGER\" />";
			}
			System.out.println(linestr);
		}

		return str;
	}

	/**
	 * 驼峰改下划线
	 */
	public static String getResultMapNew(Class<?> cls) throws Exception {
		StringBuilder str = new StringBuilder();
		// 头部 <resultMap id="BaseResultMap" type="com.huajie.entity.sys.SysMenuinfo">
		str = new StringBuilder("<resultMap id=\"" + cls.getSimpleName() + "ResultMap\" type=\"" + cls.getName() + "\"> \r\n");
		Field[] declaredFields = cls.getDeclaredFields();
		for (Field field : declaredFields) {
			if("serialVersionUID".equals(field.getName())){
				continue;
			}
			if("id".equals(field.getName())){
				str.append("<id column=\"").append(getUpCaseReplace(field.getName())).append("\" property=\"").append(field.getName()).append("\" /> \r\n");
			}else{
				str.append("<result column=\"").append(getUpCaseReplace(field.getName())).append("\" property=\"").append(field.getName()).append("\" /> \r\n");
			}
		}
		str.append("</resultMap>");
		return str.toString();
	}


	/**
	 * bean字段 sql语句字段
	 */
	public static String getColumnList(Class<?> cls) throws Exception {
		// 每一行字符串 <result column="BID_SECTION_CODE" property="BID_SECTION_CODE"
		// jdbcType="VARCHAR" />
		String linestr = "";
		Field[] declaredFields = cls.getDeclaredFields();
		for (Field field : declaredFields) {
			linestr = linestr + field.getName() + ",";
		}
		String str = linestr.substring(0, linestr.length() - 1);
		System.out.println(str);
		return str;
	}

	/**
	 * 将字符串中的驼峰写法替换成'_'
	 */
	private static String getUpCaseReplace(String str) {
		List<String> listChar = getUpCaseList(str);
		for (String s : listChar) {
			str = str.replace(s, "_" + s.toLowerCase());
		}
		return str;
	}

	/**
	 * 输出字符串中的大写字母
	 */
	private static List<String> getUpCaseList(String str) {
		List<String> listChar = new ArrayList<>();
		// 转为char数组
		char[] ch = str.toCharArray();
		// 得到大写字母
		for (char c : ch) {
			if (c >= 'A' && c <= 'Z') {
				listChar.add(String.valueOf(c));
			}
		}
		return listChar;
	}

	/**
	 * 输出字符串中的大写字母
	 */
	private static String getColumnListNew(Class<?> cls) throws Exception {
		// 每一行字符串 <result column="BID_SECTION_CODE" property="BID_SECTION_CODE"
		// jdbcType="VARCHAR" />
		String linestr = "";
		Field[] declaredFields = cls.getDeclaredFields();
		for (Field field : declaredFields) {
			linestr = linestr + getUpCaseReplace(field.getName()) + ",";
		}
		String str = linestr.substring(0, linestr.length() - 1);
		System.out.println(str);
		return str;
	}

	public static void main(String[] args) throws Exception {
		User bean = new User();
		System.out.println(getResultMapNew(bean.getClass()));
	}

}
